//
//  StatisticsMgr.m
//  carcareIOS
//
//  Created by ileo on 16/6/16.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "StatisticsMgr.h"
#import "StatisticsModel.h"
#import "AISystemInfo.h"
#import "AIUserInfo.h"
#import "NetStatistics.h"

@interface StatisticsMgr ()

@property (nonatomic, strong) StatisticsModel *model;
@property (nonatomic, strong) StatisticsDataModel *dataModel;
@property (nonatomic, strong) NSMutableArray *clickModels;

@property (nonatomic, copy) NSString *currentTitle;

@end

@implementation StatisticsMgr

IMP_SINGLETON

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.clickModels = [NSMutableArray arrayWithCapacity:3];
    }
    return self;
}

-(void)didAppear:(UIViewController *)viewController{
    if (viewController.title) {
        self.currentTitle = viewController.title;
        self.model.prePVID = self.model.pvid ? self.model.pvid : @"";
        self.model.pvid = [NSString stringWithFormat:@"ios-%@-%@",[AISystemInfo sharedInstance].version,viewController.title];
        self.model.accessTime = [self currentTime];
    }
}

-(void)willDisappear:(UIViewController *)viewController{
    if (viewController.title && [self.currentTitle isEqualToString:viewController.title]) {
        self.model.leaveTime = [self currentTime];
        self.model.uid = USERINFO.userID;
        self.dataModel.click = [self.clickModels copy];
        self.model.data = @{@"ios":self.dataModel.modelDic};
        [self.clickModels removeAllObjects];
        [NetStatistics sendStatistics:self.model.modelDic];
    }
}

-(void)addClickID:(NSString *)identify description:(NSString *)description{
    StatisticsDataClickModel *model = [[StatisticsDataClickModel alloc] init];
    model.id = identify;
    model.ele = description;
    model.time = [self currentTime];
    [self.clickModels addObject:model.modelDic];
}

-(StatisticsModel *)model{
    if (!_model) {
        _model = [[StatisticsModel alloc] init];
        _model.app = @"cz";
        _model.ubt_client_version = @"1.0.0";
        _model.ubt_client_type = @"ios";
    }
    return _model;
}

-(StatisticsDataModel *)dataModel{
    if (!_dataModel) {
        _dataModel = [[StatisticsDataModel alloc] init];
        _dataModel.version = [AISystemInfo sharedInstance].version;
    }
    return _dataModel;
}

-(NSString *)currentTime{
    return [NSString stringWithFormat:@"%.0f",[[NSDate date] timeIntervalSince1970] * 1000];
}

@end
